import publicForm from "@/components/public/el-forms.vue"; //弹框form
import publicForm1 from "@/components/public/el-form.vue"; //弹框form

import formJs from "./regexDetail.js"; //新增弹框配置项
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";
import {
    savePurveyorData, //保存
    getPurveyorDataById, //详情
    // getSpeciesDataList //种类

} from "@/api/request.js";
export default {
    // props: [
    //     "dialogVisible",
    //     "dialogTitle",
    //     "statusNum",
    //     "currentRow",
    //     "typ"
    // ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,

            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",

            currntData: null,
            dataInfo: null,
            startDates: null,
            treeData: [], //材料种类
            formData: {
                fId: "",
                fMerchantname: "", //生产厂家名/供应商名
                fUscc: "", //统一社会信用代码
                // fVarietyid: "", //生产材料种类ID，字符串'、'分隔
                // fVarietyname: "", //生产材料种类名，字符串'、'分隔
                fNote: "", //备注
                typ: "", //"1"材料生产商，"2"材料供应商
                userId: "", //当前登录账户ID
                userName: "", //当前登录账户名
                fBusinesslicense: "", //企业营业执照
                fProductionlicense: "", //生产许可证
                recordfiles: []
            },
            formObj: formJs.formObj,
            formObj1: formJs.formObj1,

            fileList: [], //企业营业执照
            fileList0: [], //生产许可证

            btnShow: true,
            currentDetail: null,
            selected: [],
            typs: null,
            currentDetail: null,
            fStatus: 1,
            primaryInfo: null,
            currentDetail: null,
            statusNum: null,
            currentRow: {}
        };
    },
    components: {
        publicForm,
        FormLyout,
        publicForm1

    },
    // watch: {
    //     dialogVisible: {
    //         handler(newVal) {
    //             if (newVal) {
    //                 this.visible = newVal;
    //             }
    //         },
    //         immediate: true,
    //     },
    //     dialogTitle: {
    //         handler(newVal) {
    //             if (newVal) {
    //                 this.title = newVal;
    //             }
    //         },
    //         immediate: true,
    //     },
    //     typ: {
    //         handler(newVal) {
    //             if (newVal) {

    //             }
    //         },
    //         immediate: true,
    //     },
    //     statusNum: {
    //         handler(newVal) {
    //             if (newVal) {

    //                 this.edit(newVal);
    //             }
    //         },
    //         immediate: true,
    //     },

    //     currentRow: {
    //         handler(newVal) {
    //             if (newVal) {
    //                 this.currentDetail = newVal;
    //                 if (this.statusNum != 1) {
    //                     this.getDetail({
    //                         fId: this.currentDetail.fId
    //                     }) // currentRow
    //                 }
    //             }
    //         },
    //         immediate: true,
    //     },

    // },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        this.statusNum = this.primaryInfo.status;

        this.currentDetail = this.primaryInfo.data;

        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser
        }, true);
        this.typs = this.primaryInfo.typ;
        if (this.typs == 1) {
            this.formObj[0] = {
                input: true, //是input
                label: "生产厂家名称", //字段
                prop: "fMerchantname", //字段名
                width: 12, //参考el-col
                disabled: false, //是否禁用
                rules: [{
                    required: true,
                    message: '生产厂家名称'
                }], //验证

            }
            this.formObj1[1] = {
                input: true, //是input
                label: "生产厂家地址", //字段
                prop: "fMerchantadree", //字段名
                width: 24, //参考el-col
                disabled: false, //是否禁用
                rules: [{
                    required: false,
                    message: '生产厂家地址'
                }], //验证

            }
        } else if (this.typs == 2) {
            this.formObj[0] = {
                input: true, //是input
                label: "供应商名称", //字段
                prop: "fMerchantname", //字段名
                width: 12, //参考el-col
                disabled: false, //是否禁用
                rules: [{
                    required: true,
                    message: '供应商名称'
                }], //验证

            }
            this.formObj1[1] = {
                input: true, //是input
                label: "供应商地址", //字段
                prop: "fMerchantadree", //字段名
                width: 24, //参考el-col
                disabled: false, //是否禁用
                rules: [{
                    required: false,
                    message: '供应商地址'
                }], //验证

            }

        }
        this.btnShow = true
        // this.getSpeciesList() //获取种类材料
        if (this.statusNum == 1) { //新增
            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj1.map((item) => {
                item.disabled = false;
            });
            this.formObj[2].uploadSelect = true;
            this.formObj1[0].uploadSelect = true;

            this.btnShow = true;

            this.formData.fBusinesslicense = 'fBusinesslicense' + new Date().getTime(); //企业营业执照
            this.formData.fProductionlicense = 'fProductionlicense' + new Date().getTime(); //生产许可证

        } else if (this.statusNum == 2) { //编辑

            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj1.map((item) => {
                item.disabled = false;
            });
            this.formObj[2].uploadSelect = true;
            this.formObj1[0].uploadSelect = true;
            this.btnShow = true

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.formObj1.map((item) => {
                item.disabled = true;
            });
            this.formObj[2].uploadSelect = false;
            this.formObj1[0].uploadSelect = false;
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.3 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        // 刪除文件
        delteFile(file, num) {
            console.log("删除照片", file);
            var data;
            if (num == 0) { //企业营业执照
                data = this.fileList;
            } else if (num == 1) { //生产许可证
                data = this.fileList0;

            }
            for (let index = 0; index < data.length; index++) {
                if (file.uid == data[index].uid) {
                    if (data[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: data[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: data[index].atta.fId,
                        });
                    }
                    data.splice(index, 1);
                }
            }
   
        },
        selectChange(data, a, b) {
            if (b == "fVarietyname") {

                this.formData.fVarietyid = data.value;
                this.formData.fVarietyname = data.label;
                this.formObj[1].options = []
                data.children.map(item => {

                    this.formObj[1].options.push({
                        label: item.fVarietyname, //种类名
                        value: item.fVarietyid, //种类id
                        fFrequency: item.fFrequency, //检测频率
                        fStandard: item.fStandard, //依据标准
                        fUnit: item.fUnit, //单位
                        fNote: item.fNote,
                    })

                })
            } else if (b == "fMaterialname") {
                this.formData.fMaterialid = data.value;
                this.formData.fMaterialname = data.label;
                this.formData.fFrequency = data.fFrequency;
                this.formData.fStandard = data.fStandard;
                this.formData.fUnit = data.fUnit;
                this.formData.fNote = data.fNote;
            }


        },
        //   edit(status)
        edit() {
            this.formData = this.$options.data.call(this).formData;

            // if (status == 1) {
            //     this.formData = this.$options.data.call(this).formData;
            // } else if (status == 2) {
            //     this.formData = this.$options.data.call(this).formData;
            // } else if (status == 4) {
            //     this.monthExamineData = this.$options.data.call(this).monthExamineData;
            // }
        },
        getSpeciesList() {
            var obj = {
                page: 1,
                pageSize: 500
            }
            getSpeciesDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.treeData = []
                        this.formObj[2].options = []
                        res.data.pageInfo.list.map(item => {
                            if (item.fStatus == 1) {
                                // this.treeData.push(item)
                                // this.treeData.push(item)
                                this.formObj[2].options.push({
                                    label: item.fVarietyname,
                                    value: item.fVarietyid,
                                    children: item.materialList
                                })
                            }
                        })


                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getPurveyorDataById(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data
                    this.formData = data
                    this.loading = false;
                    this.fileList = res.data.fBusinessLincense;
                    this.fileList0 = res.data.fProductionLicense;
                    this.formData.recordfiles=[];
                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }

                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },

        yj_saveForm(status) {
            this.formData.userId = this.userInfo.id; //当前登录人id
            this.formData.userName = this.userInfo.userName; //当前登录人名称



            // 0连续新增1新增
            // this.formData
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData, status);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(data, status) {
            this.loading = true;
            this.formData.typ = this.typs
            this.isLock = true
            savePurveyorData(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;

                        this.visible = false;
                        if (this.typs == 1) {
                            this.$router.push({
                                path: "/filingMaterialsManage/manufacturer",
                            });
                        } else {
                            this.$router.push({
                                path: "/filingMaterialsManage/supplier",
                            });
                        }
                        this.$message.success({
                            content: '新增成功',
                            duration: 1000
                        })
                        this.isLock = false
                    } else if (res.errcode == -1) {
                        this.loading = false;

                        this.visible = false;
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        this.$emit("refreshList", status);
                        this.isLock = false
                    } else {
                        this.loading = false;

                        this.isLock = false
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })

                    }
                })


        },
    },
};